package com.xinyiweidai.xinyiweidai.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lan.mylibrary.ui.MyLoadingDialog;
import com.xinyiweidai.xinyiweidai.R;

/**
 * 项目名：com.lanxiong.zhihu.base
 * 类名： BaseFragment
 * 创建人：Lan
 * 创建时间：2018/4/29 19:07
 * 描述：  Fragment基类
 */

public abstract class BaseFragment extends Fragment {


//    private MyEmptyView emptyView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        initToolBar(view);
        receiveData();
        initView(view);
        addClickEvent();
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        loadData();
    }

    protected  void initToolBar(View view){}

    protected void receiveData() {}

    protected abstract int getLayoutId();

    protected abstract void initView(View view);

    protected  void addClickEvent(){}
    /**
     * 由于不是viewpager+fragment的结构 懒加载也不需要
     * 而且当前每次只显示一个fragment 还是通过replace的方法 所以可以去掉
     */
//    protected abstract void loadData();



//    public void showError(List<?> objectList,MyEmptyView emptyView) {
//        MyToast.errorToast(getActivity(), getString(R.string.errorNet));
//        emptyView.show(objectList.size() > 0 ? "" : "reLoad");
//        emptyView.setOnReloadEvent(v -> {emptyView.show("loading");loadData();});
//    }



    public void startNewActivity(Class<?> clazz) {
        startActivity(new Intent(getActivity(), clazz));
    }

    private MyLoadingDialog dialog;


    public void showLoading(String str){
        if (dialog == null) {
            dialog = new MyLoadingDialog
                    .Builder(getActivity())
                    .setMessage(str)
                    .setCancelable(true)
                    .setCancelOutside(false)
                    .create();
        }

        TextView textView= (TextView) dialog.findViewById(R.id.tv_loading);
        textView.setText(str);
        dialog.show();
    }

    public void dismissLoading(){
        if (dialog!=null&&dialog.isShowing()){
            dialog.dismiss();
        }
    }
}
